<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]])
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$otherServiceId          = isset($_REQUEST['otherServiceId']) ? $_REQUEST['otherServiceId'] : 0;
	$rAndDComplete           = isset($_REQUEST['rAndDComplete']) ? $_REQUEST['rAndDComplete'] : 0;
  $rAndDInComplete         = isset($_REQUEST['rAndDInComplete']) ? $_REQUEST['rAndDInComplete'] : 0;
  $partySelected           = "";
  $categorySelected        = "";
  $complainDateSelected    = "";
  $complainDateDisplay     = "";
	$targetedDateSelected    = "";
	$targetedDateDisplay     = "";
	$descriptionSelected     = "";
	$importantPointSelected  = "";
	$deactive                = "D";
  $closeDate               = date('Y-m-d H:i:s');
  $print                   = isset($_REQUEST['print']) ? $_REQUEST['print'] : 0;
  
	if(isset($_POST['insertBtn']) || isset($_POST['printBtn']))
  {
	  if(isset($_REQUEST['otherServiceId']) &&  $_REQUEST['otherServiceId'] > 0)
		{
		  if($rAndDComplete == 1)
		  {
		  	$updateOtSrvComplete = "UPDATE otherservice
		                               SET description     = '".nl2br(addslashes($_POST['description']))."',
		                                   importantPoint  = '".nl2br(addslashes($_POST['importantPoint']))."',
	                                     status          = '".$deactive."',
	                                     closeDate       = '".$closeDate."'
		                             WHERE otherServiceId  = ".$_REQUEST['otherServiceId'];
		    $updateOtSrvCompleteResult = mysql_query($updateOtSrvComplete);
		  	if(!$updateOtSrvCompleteResult)
		      die("Update Query Error : ". mysql_error());
		    else
		    {
		      header("Location:completeReport.php");
		      exit();
		    }
		  }
		  elseif($rAndDInComplete == 1)
		  {
		  	$targetDateTime = $_POST['targetDateYear']."-".$_POST['targetDateMonth']."-".$_POST['targetDateDay'];
		    $updateOtSrvInComplete = "UPDATE otherservice
	                                   SET targetedDate      = '".$targetDateTime."',
	                                       description       = '".nl2br(addslashes($_POST['description']))."',
	                                       importantPoint    = '".nl2br(addslashes($_POST['importantPoint']))."'
	                                 WHERE otherServiceId =  ".$_REQUEST['otherServiceId'];
	  	  $updateOtSrvInCompleteResult = mysql_query($updateOtSrvInComplete);
		    if(!$updateOtSrvInCompleteResult)
		      die("Update Query Error : ". mysql_error());
		    else
		    {
		      header("Location:serviceList.php");
		      exit();
		    }
		  }
		  else
		  {
		  	$insertDateTime = $_POST['targetDateYear']."-".$_POST['targetDateMonth']."-".$_POST['targetDateDay'];
		  	$curDate = new DateTime();
        $curDate->setTimezone($cfgTimeZone);  //cfgTimeZone declare in config file
        $timeset = $curDate->format('H:i:s');
		    $complainDate = date("Y-m-d")." ".$timeset;
		    $updateupdateOtSrvEntry = "UPDATE otherservice
	                                    SET partyId           = '".$_POST['partyId']."',
	                                        categoryId        = '".$_POST['categoryId']."',
	                                        complainDate      = '".$complainDate."',
	                                        targetedDate      = '".$insertDateTime."',
	                                        description       = '".nl2br(addslashes($_POST['description']))."',
	                                        importantPoint    = '".nl2br(addslashes($_POST['importantPoint']))."'
	                                  WHERE otherServiceId    =  ".$_REQUEST['otherServiceId'];
	  	  $updateupdateOtSrvEntryResult = mysql_query($updateupdateOtSrvEntry) or die (mysql_error());
		    if(!$updateupdateOtSrvEntryResult)
		      die("Update Query Error : ". mysql_error());
		    else
		    {
		      header("Location:serviceRegisList.php");
		      exit();
		    }
	    }
	  }
	  else
    {
      $curDate = new DateTime();
      $curDate->setTimezone($cfgTimeZone);  //cfgTimeZone declare in config file
      $timeset = $curDate->format('H:i:s');
	    $complainDate = date("Y-m-d")." ".$timeset;
      $targetDate = $_POST['targetDateYear']."-".$_POST['targetDateMonth']."-".$_POST['targetDateDay'];
    	$insertService ="INSERT INTO otherservice(partyId,serviceType,categoryId,complainDate,targetedDate,description,importantPoint)
    									 VALUES('".$_POST['partyId']."','R&D','".$_POST['categoryId']."',
    													'".$complainDate."','".$targetDate."','".nl2br(addslashes($_POST['description']))."',
    													'".nl2br(addslashes($_POST['importantPoint']))."')";
      $insertServiceResult = mysql_query($insertService)or die(mysql_error());
      $lastnsertOtherServiceId = mysql_insert_id();
      if(isset($_POST['printBtn']))
      {
        header("Location:serviceList.php?print=1&otherServiceId=".$lastnsertOtherServiceId);
        exit();
      }
      else
      {
        header("Location:serviceList.php");
        exit();
      }
    }
  }
   
  // Selection of data while making complete or incomplete status : Starts
  if(isset($_REQUEST['otherServiceId']) && $_REQUEST['otherServiceId'] > 0)
  {
  
  	$selectOtherData = "SELECT otherservice.partyId, otherservice.categoryId, otherservice.complainDate AS complainDateSelected,
  	                           DATE_FORMAT(otherservice.complainDate,'%d-%m-%Y') AS complainDateDisplay, otherservice.targetedDate AS targetedDateSelected,
  	                           DATE_FORMAT(otherservice.targetedDate,'%d-%m-%Y') AS targetedDateDisplay, otherservice.description,
  	                           otherservice.importantPoint
  	                      FROM otherservice
  	                     WHERE otherServiceId = ".$_REQUEST['otherServiceId'];
    $selectOtherDataResult = mysql_query($selectOtherData);
    while($editDataRow = mysql_fetch_array($selectOtherDataResult))
    {
      $partySelected           = $editDataRow['partyId'];
      $categorySelected        = $editDataRow['categoryId'];
      $complainDateSelected    = $editDataRow['complainDateSelected'];
      $complainDateDisplay     = $editDataRow['complainDateDisplay'];
		  $targetedDateSelected    = $editDataRow['targetedDateSelected'];
		  $targetedDateDisplay     = $editDataRow['targetedDateDisplay'];
		  $descriptionSelected     = $editDataRow['description'];
		  $importantPointSelected  = $editDataRow['importantPoint'];
    }
  }
  // Selection of data while making complete or incomplete status : Ends
  
  $partyId       = "";
  $companyName   = "";
  $categoryId    = "";
  $categoryName  = "";
  $typeOfCallId  = "";
  $type          = "";
  
  ///// Select Party : Start
  $selectParty = "SELECT partyId,companyName
                    FROM party
                ORDER BY companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  {
    $partyId[$b]     = $partyRow['partyId'];
    $companyName[$b] = substr_replace($partyRow['companyName'],'...',40);
    $b++;
  }
  ///// Select Party : End
  ///// Select category : Start
  $selectCategary= "SELECT categoryId,categoryName
                      FROM category
                  ORDER BY categoryName";
  $technicianResult = mysql_query($selectCategary);
  $d = 0;
  while($categoryRow = mysql_fetch_array($technicianResult))
  {
    $categoryId[$d]   = $categoryRow['categoryId'];
    $categoryName[$d] = $categoryRow['categoryName'];
    $d++;
  }
  ///// Select category : End
  
  include("./bottom.php");
  $smarty->assign("otherServiceId",$otherServiceId);
  $smarty->assign("rAndDComplete",$rAndDComplete);
  $smarty->assign("rAndDInComplete",$rAndDInComplete);
  $smarty->assign("partySelected",$partySelected);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("complainDateSelected",$complainDateSelected);
  $smarty->assign("complainDateDisplay",$complainDateDisplay);
  $smarty->assign("targetedDateSelected",$targetedDateSelected);
  $smarty->assign("targetedDateDisplay",$targetedDateDisplay);
  $smarty->assign("descriptionSelected",$descriptionSelected);
  $smarty->assign("importantPointSelected",$importantPointSelected);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryName",$categoryName);
  $smarty->assign("typeOfCallId",$typeOfCallId);
  $smarty->assign("type",$type);
  $smarty->display("rAndD.tpl");
}
?>